package com.jcl.recruit.verfity.service;

import com.jcl.core.utils.CodeUtils;
import com.jcl.recruit.console.logic.VerificationCodeLogic;
import com.jcl.recruit.console.logic.VerificationValidateLogic;
import com.jcl.recruit.console.model.ConsoleVerificationCode;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.email.logic.SendEmailHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName VerificationService

 * @Author yyliu
 * @Date 2018/9/18 9:51
 * @Version 1.0
 **/
@Service
public class VerificationService {

    @Autowired
    private VerificationCodeLogic verificationCodeLogic;

    @Autowired
    private VerificationValidateLogic verificationValidateLogic;

    @Autowired
    private SendEmailHandler sendEmailHandler;

    public int sendPhoneRegisterCode(String mobilePhone) {
        String code = CodeUtils.generateVerificationCodeDefault();
        int success = 0;

        if (0 == success) {
            verificationCodeLogic.addVerificationCode(mobilePhone, code,
                    Constants.VerificationInfo.VERIFY_TYPE_PHONE_REGISTER,
                    Constants.VerificationInfo.VERIFY_ABATE_PHONE_MINUTE);
        }

        return success;

    }

    public int sendEmailRegisterCode(String email) {

        String code = CodeUtils.generateVerificationCodeDefault();

        int success = sendEmailHandler.sendEmailRegisterCode(email, code);

        if (0 == success) {
            verificationCodeLogic.addVerificationCode(email, code,
                    Constants.VerificationInfo.VERIFY_TYPE_EMAIL_REGISTER,
                    Constants.VerificationInfo.VERIFY_ABATE_EMAIL_MINUTE);
        }

        return success;
    }

    public int validateEmailRegisterCode(String email, String code) {
        ConsoleVerificationCode verificationCode = verificationCodeLogic.validateCode(email,
                Constants.VerificationInfo.VERIFY_TYPE_EMAIL_REGISTER, code);

        return verificationValidateLogic.validateCode(verificationCode);
    }

    public int validatePhoneRegisterCode(String phone, String code) {
        ConsoleVerificationCode verificationCode = verificationCodeLogic.validateCode(phone,
                Constants.VerificationInfo.VERIFY_TYPE_PHONE_REGISTER, code);

        return verificationValidateLogic.validateCode(verificationCode);
    }


}
